#include "qwtplot.h"
qwtPlot::qwtPlot(QWidget *parent) :
    QwtPlot(parent)
{
//    setTitle("图的标题");
    //---------设置画布---------//

}

void qwtPlot::paintPlot(QPolygonF point,QPolygonF point2)
{
    //-----------------开始画图----------------------//
    QwtPlotCurve *curve=new QwtPlotCurve("_电阻率曲线_");
    // curve->setTitle( "信道"+QString( "%1 " ).arg( i+1));
    curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
                                       QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
    QwtSymbol *symbol2 = new QwtSymbol( QwtSymbol::Ellipse,
                                       QBrush( Qt::blue ), QPen( Qt::red, 2 ), QSize( 10, 10) );//设置样本点的颜色、大小

    curve->setSymbol(symbol);//添加样本点形状


//        point2<<QPointF(1,2)<<QPointF(2,3)<<QPointF(3,4)<<QPointF(4,5)<<QPointF(5,6)<<QPointF(6,7)<<QPointF(7,8);
    curve->setSamples(point);
    curve->attach(this);
    curve->hide();

//        curve->setLegendAttribute(curve->LegendShowLine);//显示图例的标志，这里显示线的颜色。

    //    //曲线2的形状采用默认，即不单独设置画笔的颜色、样本点的显示
        QwtPlotCurve *curve2=new QwtPlotCurve("+当前样点");
        curve2->setSamples(point2);
        curve2->setSymbol(symbol2);//添加样本点形状
        curve2->attach( this );
//        curve2->setLegendAttribute(curve->LegendShowLine);

    //--------------设置图例可以被点击来确定是否显示曲线-----------------------//
    QwtLegend *legend = new QwtLegend;
    //    legend->setDefaultItemMode( QwtLegendData::Checkable );//图例可被点击
    //    insertLegend( legend, QwtPlot::RightLegend );
    //    connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),
    //             SLOT( showItem( const QVariant &, bool ) ) );//点击图例操作

    QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状，注意改变参数
    //  qDebug()<<items;

    for ( int i = 0; i < items.size(); i++ )
    {

        if ( i == 0 )
        {
            const QVariant itemInfo = itemToInfo( items[i] );

            QwtLegendLabel *legendLabel =
                    qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) );
            if ( legendLabel )
                legendLabel->setChecked( true );//

            items[i]->setVisible( true );
        }
        else
        {
            items[i]->setVisible( true );
        }
    }


    this->resize(600,400);

    this->replot();

    setAutoReplot( true );//设置自动重画，相当于更新
}

void qwtPlot::initialization(double xmin, double xmax, double ymin, double ymax)
{
    QwtPlotCanvas *canvas=new QwtPlotCanvas();
    canvas->setPalette(Qt::white);
    canvas->setBorderRadius(10);
    setCanvas( canvas );
    plotLayout()->setAlignCanvasToScales( true );

    //-----------设置x，y坐标和范围--------------//
    setAxisTitle( QwtPlot::yLeft, "_电阻率Ω·m_" );
    setAxisTitle( QwtPlot::xBottom, "_测点号_" );
    setAxisScale(QwtPlot::yLeft,ymin,ymax);
    setAxisScale(QwtPlot::xBottom,xmin,xmax);

    //----------------设置栅格线-------------------//
    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->enableX( true );//设置网格线
    grid->enableY( true );
    grid->setMajorPen( Qt::black, 0, Qt::DotLine );
    grid->attach( this );

    //图形缩放
    QwtPlotZoomer *zoomer = new QwtPlotZoomer( canvas );                     //选择图形局部放大
    zoomer->setRubberBandPen( QColor( Qt::blue ) ); //勾选扩大区域边界颜色
    zoomer->setMousePattern( QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier );//ctrl+右键==回复到原始状态
    zoomer->setMousePattern( QwtEventPattern::MouseSelect3, Qt::RightButton );       //右键==恢复到上一次扩大之前的状态

    QwtPlotPanner *panner = new QwtPlotPanner( canvas );                                   //默认的左键移动功能
    panner->setMouseButton( Qt::RightButton );        //设置哪个按钮移动画布  如果不设置(注册掉当前行)按钮默认为左键为移动

    QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas );                 //默认的滑轮及右键缩放功能  图形的整体缩放
    magnifier->setMouseButton(Qt::LeftButton);     //设置哪个按钮与滑轮为缩放画布  如果不设置(注册掉当前行)按钮默认为滑轮以及右键为缩放
}

void qwtPlot::initializationSeven(double xmin, double xmax, double ymin, double ymax)
{
    QwtPlotCanvas *canvas=new QwtPlotCanvas();
    canvas->setPalette(Qt::white);
    canvas->setBorderRadius(10);
    setCanvas( canvas );
    plotLayout()->setAlignCanvasToScales( true );

    //-----------设置x，y坐标和范围--------------//
//    setAxisTitle( QwtPlot::yLeft, "_电阻率Ω·m_" );
//    setAxisTitle( QwtPlot::xBottom, "_测点号_" );
    setAxisScale(QwtPlot::yLeft,ymin,ymax);
    setAxisScale(QwtPlot::xBottom,xmin,xmax);
    enableAxis(QwtPlot::yLeft,false);
    enableAxis(QwtPlot::xBottom,false);

    //----------------设置栅格线-------------------//
    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->enableX( true );//设置网格线
    grid->enableY( true );
    grid->setMajorPen( Qt::black, 0, Qt::DotLine );
    grid->attach( this );

    //图形缩放
    QwtPlotZoomer *zoomer = new QwtPlotZoomer( canvas );                     //选择图形局部放大
    zoomer->setRubberBandPen( QColor( Qt::blue ) ); //勾选扩大区域边界颜色
    zoomer->setMousePattern( QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier );//ctrl+右键==回复到原始状态
    zoomer->setMousePattern( QwtEventPattern::MouseSelect3, Qt::RightButton );       //右键==恢复到上一次扩大之前的状态

    QwtPlotPanner *panner = new QwtPlotPanner( canvas );                                   //默认的左键移动功能
    panner->setMouseButton( Qt::RightButton );        //设置哪个按钮移动画布  如果不设置(注册掉当前行)按钮默认为左键为移动

    QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas );                 //默认的滑轮及右键缩放功能  图形的整体缩放
    magnifier->setMouseButton(Qt::LeftButton);     //设置哪个按钮与滑轮为缩放画布  如果不设置(注册掉当前行)按钮默认为滑轮以及右键为缩放
}

void qwtPlot::hidePlot()
{
    QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状，注意改变参数
    //  qDebug()<<items;

    for ( int i = 0; i < items.size(); i++ )
    {
            items[i]->setVisible(false);
    }

}

void qwtPlot::paintSevenPlot(QPolygonF point0,QPolygonF point1,QPolygonF point2,QPolygonF point3,QPolygonF point4,QPolygonF point5,QPolygonF point6)
{
    //-----------------开始画图----------------------//
    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
                                       QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
    QwtPlotCurve *curve=new QwtPlotCurve("_电阻率曲线_");
    curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve->setSymbol(symbol);//添加样本点形状
    curve->setSamples(point0);
    curve->attach(this);
    curve->hide();

    QwtPlotCurve *curve2=new QwtPlotCurve("+当前样点");
    curve2->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve2->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve2->setSamples(point1);
    curve2->setSymbol(symbol);//添加样本点形状
    curve2->attach(this);

    QwtPlotCurve *curve3=new QwtPlotCurve("+当前样点");
    curve3->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve3->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve3->setSamples(point2);
    curve3->setSymbol(symbol);//添加样本点形状
    curve3->attach(this);

    QwtPlotCurve *curve4=new QwtPlotCurve("+当前样点");
    curve4->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve4->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve4->setSamples(point3);
    curve4->setSymbol(symbol);//添加样本点形状
    curve4->attach(this);

    QwtPlotCurve *curve5=new QwtPlotCurve("+当前样点");
    curve5->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve5->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve5->setSamples(point4);
    curve5->setSymbol(symbol);//添加样本点形状
    curve5->attach(this);

    QwtPlotCurve *curve6=new QwtPlotCurve("+当前样点");
    curve6->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve6->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve6->setSamples(point5);
    curve6->setSymbol(symbol);//添加样本点形状
    curve6->attach(this);

    QwtPlotCurve *curve7=new QwtPlotCurve("+当前样点");
    curve7->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve7->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
    curve7->setSamples(point6);
    curve7->setSymbol(symbol);//添加样本点形状
    curve7->attach(this);

//    //--------------设置图例可以被点击来确定是否显示曲线-----------------------//
//    QwtLegend *legend = new QwtLegend;
//    QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状，注意改变参数
//    for ( int i = 0; i < items.size(); i++ )
//    {
//        if ( i == 0 )
//        {
//            const QVariant itemInfo = itemToInfo( items[i] );
//            QwtLegendLabel *legendLabel =
//                    qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) );
//            if ( legendLabel )
//                legendLabel->setChecked( true );//
//            items[i]->setVisible( true );
//        }
//        else
//        {
//            items[i]->setVisible( true );
//        }
//    }
    this->resize(600,400);
    this->replot();
    setAutoReplot( true );//设置自动重画，相当于更新
}
